package com.droidlogic.mboxlauncher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.SearchManager;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.media.tv.TvView;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
//import android.os.ServiceManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
//import android.view.IWindowManager;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Launcher extends Activity {
    public static String COMPONENT_LIVE_TV = "com.android.tv/com.android.tv.TvActivity";
    public static String COMPONENT_TV_APP = "com.droidlogic.tvsource/com.droidlogic.tvsource.DroidLogicTv";
    public static String COMPONENT_TV_SETTINGS = "com.android.tv.settings/com.android.tv.settings.MainSettings";
    public static String DEFAULT_INPUT_ID = "com.droidlogic.tvinput/.services.ATVInputService/HW0";
    public static int HOME_SHORTCUT_COUNT = 10;
    public static String PACKAGE_MFC = "com.valor.mfc.droid.tvapp.generic";
    private static final int[] childScreens = {1, 2, 4, 3, 5};
    private static final int[] childScreensTv = {2, 4, 3, 5};
    public static float endX;
    public static boolean isLaunchingThomasroom = false;
    public static boolean isLaunchingTvSettings = false;
    private static float scale_value;
    public static float startX;
    private final String DROIDVOLD_MEDIA_EJECT_ACTION = "com.droidvold.action.MEDIA_EJECT";
    private final String DROIDVOLD_MEDIA_MOUNTED_ACTION = "com.droidvold.action.MEDIA_MOUNTED";
    private final String DROIDVOLD_MEDIA_UNMOUNTED_ACTION = "com.droidvold.action.MEDIA_UNMOUNTED";
    private BroadcastReceiver appReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("android.intent.action.PACKAGE_CHANGED".equals(action) || "android.intent.action.PACKAGE_REMOVED".equals(action) || "android.intent.action.PACKAGE_ADDED".equals(action)) {
                Launcher.this.updateAppList(intent);
            }
        }
    };
    /* access modifiers changed from: private */
    public int current_screen_mode = 0;
    private BroadcastReceiver instabootReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            if ("com.droidlogic.instaboot.RELOAD_APP_COMPLETED".equals(intent.getAction())) {
                Log.e("MediaBoxLauncher", "reloadappcompleted");
                Launcher.this.displayShortcuts();
            }
        }
    };
    private final static String TAG = "Launcher";
    private boolean isRadioChannel = false;
    private GridView lv_status;
    private AppDataLoader mAppDataLoader;
    private MyRelativeLayout mAppView;
    private MyRelativeLayout mBrowserView;
    /* access modifiers changed from: private */
    public Uri mChannelUri;
    private int[] mChildScreens = childScreens;
    private MyRelativeLayout mCleanmemoryView;
    public Context mContext;
    private CustomView mCustomView = null;
    private MyRelativeLayout mGoogleplayView;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            Log.e(TAG,"msg:"+msg.toString());
            switch (msg.what) {
                case 0:
                    resetShortcutScreen(msg.arg1);
                    break;
                case 1:
                    resetShortcutScreen(Launcher.this.current_screen_mode);
                    break;
                case 2:
                    CustomScreen((View) msg.obj);
                    break;
            }
        }
    };

    private MyGridLayout mHomeShortcutView = null;
    private ViewGroup mHomeView = null;
    private HoverView mHoverView;
    private MyRelativeLayout mKdplayerView;
    private MyRelativeLayout mLocalView;
    private MyRelativeLayout mMusicView;
    private MyRelativeLayout mRecommendView;
    private AppLayout mSecondScreen = null;
    private MyRelativeLayout mSettingsView;
    private StatusLoader mStatusLoader;
    @SuppressLint("HandlerLeak")
    public Handler mTvHandler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (Launcher.this.isBootvideoStopped()) {
                        Log.d(TAG, "======== bootvideo is stopped, start tv play");
                        Launcher.this.tuneTvView();
                        return;
                    }
                    Log.d(TAG, "======== bootvideo is not stopped, wait it");
                    Launcher.this.mTvHandler.sendEmptyMessageDelayed(0, 200);
                    return;
                case 1:
                    if (Launcher.this.isBootvideoStopped()) {
                        Log.d("MediaBoxLauncher", "======== bootvideo is stopped, start tv app");
                        Launcher.this.startTvApp();
                        Launcher.this.finish();
                        return;
                    }
                    Log.d("MediaBoxLauncher", "======== bootvideo is not stopped, wait it");
                    Launcher.this.mTvHandler.sendEmptyMessageDelayed(1, 50);
                    return;
                default:
                    return;
            }
        }
    };
    /* access modifiers changed from: private */
    public String mTvInputId;
    private int mTvTop = -1;
    private MyRelativeLayout mVideoView;
//    private IWindowManager mWindowManager;
    private BroadcastReceiver mediaReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action != null) {
                if ("android.intent.action.MEDIA_UNMOUNTED".equals(action)
                        || "android.intent.action.MEDIA_MOUNTED".equals(action)) {
                    Launcher.this.displayStatus();
                    Launcher.this.updateStatus();
                }
            }
        }
    };
    private Object mlock = new Object();
    private BroadcastReceiver netReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action != null) {
                if ("android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE".equals(action) || "android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE".equals(action)) {
                    Launcher.this.updateAppList(intent);
                } else {
                    Launcher.this.displayStatus();
                    Launcher.this.updateStatus();
                }
            }
        }
    };
    private final String net_change_action = "android.net.conn.CONNECTIVITY_CHANGE";
    private final String outputmode_change_action = "android.amlogic.settings.CHANGE_OUTPUT_MODE";
    /* access modifiers changed from: private */
    public ImageButton pg_favorite;
    /* access modifiers changed from: private */
    public ImageButton pg_home;
    private View saveHomeFocusView = null;
    private int saveModeBeforeCustom = 0;
    private TextView tvPrompt = null;
    /* access modifiers changed from: private */
    public TvView tvView = null;
    public int tvViewMode = -1;
    private final String wifi_signal_action = "android.net.wifi.RSSI_CHANGED";

    public void onCreate(Bundle savedInstanceState) {
        Log.d("renjun", "------onCreate");
//        Settings.Global.putInt(getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1);
//        Settings.Secure.putInt(getContentResolver(), "user_setup_complete", 1);
        super.onCreate(savedInstanceState);
        Object ServiceManager = null;
        try {
            Method getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
            ServiceManager = getServiceMethod.invoke(null, new Object[]{"tv_input"});
        }catch (Exception e){
//            e.printStackTrace();
//            Log.e("renjun","tv_input ServiceManager is null");
        }
        Log.d("renjun","ServiceManager="+ServiceManager);
        if (ServiceManager == null) {
            setContentView(R.layout.main_box);
        } else {
            setContentView(R.layout.main);
        }
        this.mContext = getApplicationContext();
        displaySystemInfo();
        if (Build.VERSION.SDK_INT >= 25) {
            COMPONENT_TV_APP = COMPONENT_LIVE_TV;
        }
        this.mAppDataLoader = new AppDataLoader(this);
        this.mStatusLoader = new StatusLoader(this);
//        this.mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
        scale_value = getAnimationScaleValue();
        this.mAppDataLoader.update();
        initChildViews();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.MEDIA_EJECT");
        filter.addAction("android.intent.action.MEDIA_UNMOUNTED");
        filter.addAction("android.intent.action.MEDIA_MOUNTED");
        filter.addAction("com.droidvold.action.MEDIA_UNMOUNTED");
        filter.addAction("com.droidvold.action.MEDIA_MOUNTED");
        filter.addAction("com.droidvold.action.MEDIA_EJECT");
        filter.addDataScheme("file");
        registerReceiver(this.mediaReceiver, filter);
        IntentFilter filter2 = new IntentFilter();
        filter2.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        filter2.addAction("android.net.wifi.RSSI_CHANGED");
        filter2.addAction("android.amlogic.settings.CHANGE_OUTPUT_MODE");
        filter2.addAction("android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE");
        filter2.addAction("android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE");
        registerReceiver(this.netReceiver, filter2);
        IntentFilter filter3 = new IntentFilter("android.intent.action.PACKAGE_ADDED");
        filter3.addAction("android.intent.action.PACKAGE_REMOVED");
        filter3.addAction("android.intent.action.PACKAGE_CHANGED");
        filter3.addDataScheme("package");
        registerReceiver(this.appReceiver, filter3);
        IntentFilter filter4 = new IntentFilter();
        filter4.addAction("com.droidlogic.instaboot.RELOAD_APP_COMPLETED");
        registerReceiver(this.instabootReceiver, filter4);
        this.pg_home = (ImageButton) findViewById(R.id.pg_home);
        this.pg_favorite = (ImageButton) findViewById(R.id.pg_favorite);
        this.pg_home.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View arg0, boolean hasFocus) {
                if (hasFocus) {
                    Log.v("renjun", "111111111111111111111111");
                } else {
                    Log.v("renjun", "222222222222222222222222");
                }
            }
        });
        this.pg_favorite.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View arg0, boolean hasFocus) {
                if (hasFocus) {
                    Log.v("renjun", "333333333333333333333333");
                    Launcher.this.pg_favorite.setBackground(Launcher.this.mContext.getResources().getDrawable(R.drawable.favorite_green));
                    Launcher.this.pg_home.setBackground(Launcher.this.mContext.getResources().getDrawable(R.drawable.home_white));
                    return;
                }
                Launcher.this.pg_home.setBackground(Launcher.this.mContext.getResources().getDrawable(R.drawable.home_green));
                Launcher.this.pg_favorite.setBackground(Launcher.this.mContext.getResources().getDrawable(R.drawable.favorite_white));
                Log.v("renjun", "444444444444444444444444");
            }
        });
        this.pg_favorite.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Launcher.this.setHomeViewVisible(false);
                Launcher.this.setShortcutScreen(2);
            }
        });
    }

    private void displaySystemInfo() {
        try {
            String pwd = Settings.System.getString(this.mContext.getContentResolver(), "pcba_company");
            if (!pwd.equals("txcz")) {
                System.exit(0);
            }
            Log.e("MediaBoxLauncher", pwd);
        } catch (Exception e) {
//            e.printStackTrace();
            Log.e("MediaBoxLauncher","pwd is null");
        }
    }

    public boolean isMboxFeture() {
        return true;
    }

    public boolean isTvFeture() {
        return false;
    }

    public boolean needPreviewFeture() {
        return false;
    }

    private void releasePlayingTv() {
        Log.d("MediaBoxLauncher", "------releasePlayingTv");
        recycleBigBackgroundDrawable();
        this.mTvHandler.removeMessages(0);
        releaseTvView();
    }

    /* access modifiers changed from: protected */
    public void onStart() {
        super.onStart();
    }

    /* access modifiers changed from: protected */
    @SuppressLint("WrongConstant")
    public void onResume() {
        super.onResume();
        Log.d("MediaBoxLauncher", "------onResume");
        if (checkNeedStartTvApp()) {
            this.mTvHandler.sendEmptyMessage(1);
        }
        if (isMboxFeture()) {
            setRequestedOrientation(Configuration.ORIENTATION_UNDEFINED);
        }
        if (isTvFeture()) {
            stopMusicPlayer();
        }
        setBigBackgroundDrawable();
        displayShortcuts();
        displayStatus();
//        displayDate();
        if (needPreviewFeture()) {
            this.tvView.setVisibility(View.VISIBLE);
            this.mTvHandler.sendEmptyMessage(0);
        } else if (this.tvView != null) {
            this.tvView.setVisibility(View.INVISIBLE);
        }
    }

    /* access modifiers changed from: protected */
    public void onPause() {
        super.onPause();
        this.mHandler.removeMessages(2);
        Log.d("MediaBoxLauncher", "------onPause");
        if (!needPreviewFeture()) {
            return;
        }
        if (isLaunchingThomasroom || isLaunchingTvSettings || this.mSecondScreen.getVisibility() == View.VISIBLE) {
            releasePlayingTv();
            isLaunchingThomasroom = false;
            isLaunchingTvSettings = false;
        }
    }

    /* access modifiers changed from: protected */
    public void onStop() {
        super.onStop();
        Log.d("MediaBoxLauncher", "------onStop");
        if (needPreviewFeture()) {
            releasePlayingTv();
        }
    }

    /* access modifiers changed from: protected */
    public void onDestroy() {
        unregisterReceiver(this.mediaReceiver);
        unregisterReceiver(this.netReceiver);
        unregisterReceiver(this.appReceiver);
        unregisterReceiver(this.instabootReceiver);
        super.onDestroy();
    }

    /* access modifiers changed from: protected */
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e("Launcher","onNewIntent>>"+intent.getAction());
        if ("android.intent.action.MAIN".equals(intent.getAction())) {
            setHomeViewVisible(true);
            this.current_screen_mode = 0;
            findViewById(R.id.layout_video).requestFocus();
        } else if ("android.intent.action.ALL_APPS".equals(intent.getAction())) {
            Log.e("Launcher","apps>>>>>>>>>>>");
            setHomeViewVisible(false);
            setShortcutScreen(4);
        }
    }

    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == 0) {
            startX = ev.getX();
        } else if (ev.getAction() == 1) {
            endX = ev.getX();
        }
        return super.dispatchTouchEvent(ev);
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == 1) {
        }
        return true;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == 4) {
            switch (this.current_screen_mode) {
                case 1:
                case 2:
                case 3:
                case 5:
                    break;
                case 4:
                    this.mSecondScreen.clearAnimation();
                    break;
                case 6:
                    this.current_screen_mode = this.saveModeBeforeCustom;
                    this.mAppDataLoader.update();
                    break;
            }
            setHomeViewVisible(true);
            setHomeViewVisible(true);
            this.current_screen_mode = 0;
            findViewById(R.id.layout_video).requestFocus();
            return true;
        } else if (keyCode == 23 || keyCode == 66 || keyCode != 84) {
            return super.onKeyDown(keyCode, event);
        } else {

            ComponentName globalSearchActivity = ((SearchManager) getSystemService(Context.SEARCH_SERVICE)).getGlobalSearchActivity();
            if (globalSearchActivity == null) {
                return false;
            }
            Intent intent = new Intent("android.search.action.GLOBAL_SEARCH");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(globalSearchActivity);
            Bundle appSearchData = new Bundle();
            appSearchData.putString("source", "launcher-search");
            intent.putExtra("app_data", appSearchData);
            try {
                startActivity(intent);
            }catch (Exception e){
                e.printStackTrace();
            }

            return true;
        }
    }

    /* access modifiers changed from: private */
    public void displayStatus() {
        Log.e(TAG,"displayStatus>>");
        this.lv_status.setAdapter(new LocalAdapter(this,
                this.mStatusLoader.getStatusData(),
                R.layout.homelist_item,
                new String[]{"item_icon"},
                new int[]{R.id.item_type}));
    }
    @SuppressLint("ClickableViewAccessibility")
    private void initChildViews() {
        Log.e(TAG,"initChildViews>>");
        this.lv_status = (GridView) findViewById(R.id.list_status);
        this.lv_status.setFocusable(false);
        this.lv_status.setFocusableInTouchMode(false);

        this.lv_status.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == 2) {
                    return true;
                }
                return false;
            }
        });
        this.mHoverView = (HoverView) findViewById(R.id.hover_view);
        this.mHomeView = (ViewGroup) findViewById(R.id.layout_homepage);
        this.mSecondScreen = (AppLayout) findViewById(R.id.second_screen);
        this.mHomeShortcutView = (MyGridLayout) findViewById(R.id.gv_shortcut);
        this.mVideoView = (MyRelativeLayout) findViewById(R.id.layout_video);
        this.mRecommendView = (MyRelativeLayout) findViewById(R.id.layout_recommend);
        this.mMusicView = (MyRelativeLayout) findViewById(R.id.layout_music);
        this.mAppView = (MyRelativeLayout) findViewById(R.id.layout_app);
        this.mLocalView = (MyRelativeLayout) findViewById(R.id.layout_local);
        this.mSettingsView = (MyRelativeLayout) findViewById(R.id.layout_setting);
        this.mGoogleplayView = (MyRelativeLayout) findViewById(R.id.layout_googleplay);
        this.mBrowserView = (MyRelativeLayout) findViewById(R.id.layout_browser);
        this.mKdplayerView = (MyRelativeLayout) findViewById(R.id.layout_kodi);
        this.mCleanmemoryView = (MyRelativeLayout) findViewById(R.id.layout_memory);
        setHomeRectType();
        this.tvView = (TvView) findViewById(R.id.tv_view);
        this.tvPrompt = (TextView) findViewById(R.id.tx_tv_prompt);
        if (needPreviewFeture()) {
            this.mChildScreens = childScreensTv;
            setTvView();
            return;
        }
        this.mChildScreens = childScreens;
        if (this.tvView != null) {
            this.tvView.setVisibility(View.GONE);
        }
        this.tvPrompt.setVisibility(View.GONE);
    }

    private void setBigBackgroundDrawable() {
        getMainView().setBackgroundDrawable(getResources().getDrawable(R.drawable.bg));
        ((ImageView) findViewById(R.id.img_googleplay)).setImageDrawable(getResources().getDrawable(R.drawable.img_googleplay));
        ((ImageView) findViewById(R.id.img_browser)).setImageDrawable(getResources().getDrawable(R.drawable.img_browser));
        ((ImageView) findViewById(R.id.img_setting)).setImageDrawable(getResources().getDrawable(R.drawable.img_setting));
        ((ImageView) findViewById(R.id.img_kodi)).setImageDrawable(getResources().getDrawable(R.drawable.img_kodi));
        ((ImageView) findViewById(R.id.img_memory)).setImageDrawable(getResources().getDrawable(R.drawable.img_memory));
        ((ImageView) findViewById(R.id.img_video)).setImageDrawable(getResources().getDrawable(R.drawable.img_video));
        ((ImageView) findViewById(R.id.img_recommend)).setImageDrawable(getResources().getDrawable(R.drawable.img_recommend));
        ((ImageView) findViewById(R.id.img_music)).setImageDrawable(getResources().getDrawable(R.drawable.img_music));
        ((ImageView) findViewById(R.id.img_app)).setImageDrawable(getResources().getDrawable(R.drawable.img_app));
        ((ImageView) findViewById(R.id.img_local)).setImageDrawable(getResources().getDrawable(R.drawable.img_local));
    }

    private void recycleBigBackgroundDrawable() {
        Drawable drawable = getMainView().getBackground();
        getMainView().setBackgroundResource(0);
        if (drawable != null) {
            drawable.setCallback(null);
        }
        Drawable drawable2 = ((ImageView) findViewById(R.id.img_video)).getDrawable();
        if (drawable2 != null) {
            drawable2.setCallback(null);
        }
    }

    private void setHomeRectType() {
        this.mVideoView.setType(0);
        this.mMusicView.setType(2);
        this.mRecommendView.setType(1);
        this.mAppView.setType(3);
        this.mLocalView.setType(4);
        this.mGoogleplayView.setType(8);
        this.mBrowserView.setType(9);
        this.mKdplayerView.setType(10);
        this.mCleanmemoryView.setType(11);
        Intent intent = new Intent();
        intent.setComponent(ComponentName.unflattenFromString(COMPONENT_TV_SETTINGS));
        this.mSettingsView.setType(5);
        this.mSettingsView.setIntent(intent);
    }

    public void displayShortcuts() {
        this.mAppDataLoader.update();
        switch (this.current_screen_mode) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                setShortcutScreen(this.current_screen_mode);
                return;
            default:
                setShortcutScreen(this.saveModeBeforeCustom);
                return;
        }
    }

    /* access modifiers changed from: private */
    public void updateStatus() {
        ((BaseAdapter) this.lv_status.getAdapter()).notifyDataSetChanged();
    }

    public int getCurrentScreenMode() {
        return this.current_screen_mode;
    }

    public void setShortcutScreen(int mode) {
        resetShortcutScreen(mode);
        this.current_screen_mode = mode;
    }

    public void resetShortcutScreen(int mode) {
        this.mHandler.removeMessages(0);
        Log.d("MediaBoxLauncher", "resetShortcutScreen mode is " + mode);
        if (!this.mAppDataLoader.isDataLoaded()) {
            Message msg = new Message();
            msg.what = 0;
            msg.arg1 = mode;
            this.mHandler.sendMessageDelayed(msg, 100);
        } else if (mode == 0) {
            this.mHomeShortcutView.setLayoutView(mode, this.mAppDataLoader.getShortcutList(mode));
        } else {
            this.mSecondScreen.setLayout(mode, this.mAppDataLoader.getShortcutList(mode));
        }
    }

    private int getChildModeIndex() {
        for (int i = 0; i < this.mChildScreens.length; i++) {
            if (this.current_screen_mode == this.mChildScreens[i]) {
                return i;
            }
        }
        return -1;
    }

    public AppDataLoader getAppDataLoader() {
        return this.mAppDataLoader;
    }

    public void switchSecondScren(int animType) {
        int mode;
        if (animType == 0) {
            mode = this.mChildScreens[((getChildModeIndex() + this.mChildScreens.length) - 1) % this.mChildScreens.length];
        } else {
            mode = this.mChildScreens[(getChildModeIndex() + 1) % this.mChildScreens.length];
        }
        this.mSecondScreen.setLayoutWithAnim(animType, mode, this.mAppDataLoader.getShortcutList(mode));
        this.current_screen_mode = mode;
    }

    public void setHomeViewVisible(boolean isShowHome) {
        if (isShowHome) {
            if (this.mCustomView != null && this.current_screen_mode == 6) {
                this.mCustomView.recoverMainView();
            }
            this.current_screen_mode = 0;
            this.mSecondScreen.setVisibility(View.GONE);
            this.mHomeView.setVisibility(View.VISIBLE);
            if (needPreviewFeture()) {
                setTvViewPosition(0);
                return;
            }
            return;
        }
        this.mHomeView.setVisibility(View.GONE);
        this.mSecondScreen.setVisibility(View.VISIBLE);
        if (needPreviewFeture()) {
            setTvViewPosition(2);
        }
    }

    public HoverView getHoverView() {
        return this.mHoverView;
    }

    public ViewGroup getMainView() {
        return (ViewGroup) findViewById(R.id.layout_main);
    }

    public ViewGroup getRootView() {
        return (ViewGroup) findViewById(R.id.layout_root);
    }

    public Object getLock() {
        return this.mlock;
    }

    public void saveHomeFocus(View view) {
        this.saveHomeFocusView = view;
    }

    /* access modifiers changed from: private */
    public void updateAppList(Intent intent) {
        String packageName;
        if (intent.getData() == null || !((packageName = intent.getData().getSchemeSpecificPart()) == null || packageName.length() == 0 || packageName.equals("com.android.provision"))) {
            displayShortcuts();
        }
    }
//        this.mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
    private float getAnimationScaleValue() {

        try {
            Method getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
            Object ServiceManager = getServiceMethod.invoke(null, new Object[]{"window"});
            Class<?> cStub =  Class.forName("android.view.IWindowManager$Stub");
            Method asInterface = cStub.getMethod("asInterface", IBinder.class);
            Object iWindowManager = asInterface.invoke(null, ServiceManager);
            Method getAnimationScale = iWindowManager.getClass().getMethod("getAnimationScale",int.class);
            return (float) getAnimationScale.invoke(iWindowManager,1);
        } catch (NoSuchMethodException
                | ClassNotFoundException
                | IllegalAccessException
                | InvocationTargetException e) {
            e.printStackTrace();
        }
        return 0.0f;
    }

    public void startMFC() {
        Intent intent = getPackageManager().getLaunchIntentForPackage(PACKAGE_MFC);
        if (intent != null) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory("android.intent.category.LAUNCHER");
            intent.setAction("android.intent.action.MAIN");
            try {
                startActivity(intent);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    public void startTvApp() {
        try {
            Intent intent = new Intent();
            intent.setComponent(ComponentName.unflattenFromString(COMPONENT_TV_APP));
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Log.e("MediaBoxLauncher", " can't start TvSettings:" + e);
        }
    }

    private boolean checkNeedStartTvApp() {
        return false;
    }

    public void startCustomScreen(View view) {
        Log.e("Launcher","startCustomScreen>>");
        Message msg = mHandler.obtainMessage();
        msg.what = 2;
        msg.obj = view;
        this.mHandler.sendMessageDelayed(msg, 500);
    }

    public void CustomScreen(View view) {
        Log.d("Launcher","current_screen_mode="+current_screen_mode);
        if (this.current_screen_mode != 6) {
            this.mHoverView.clear();
            if (needPreviewFeture()) {
                hideTvViewForCustom();
            }
            this.saveModeBeforeCustom = this.current_screen_mode;
            this.mCustomView = new CustomView(this, view, this.current_screen_mode);
            this.current_screen_mode = 6;
            Rect rect = new Rect();
            view.getGlobalVisibleRect(rect);
            if (rect.top > getResources().getDisplayMetrics().heightPixels / 2) {
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-2, -2);
                lp.addRule(12);
                getRootView().addView(this.mCustomView, lp);
            } else {
                getRootView().addView(this.mCustomView);
            }
            getMainView().bringToFront();
        }
    }

    public void recoverFromCustom() {
        this.mHandler.sendEmptyMessage(1);
        if (needPreviewFeture()) {
            recoverTvViewForCustom();
        }

        getMainView().setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        getMainView().requestFocus();
    }

    public static int dipToPx(Context context, float dpValue) {
        return (int) ((dpValue * context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    private void setTvView() {
        ((TextView) findViewById(R.id.tx_video)).setText(R.string.str_tvapp);
        this.tvView.setVisibility(View.VISIBLE);
        this.tvView.setCallback(new TvViewInputCallback());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            this.tvView.setZOrderMediaOverlay(false);
        }
        setTvViewPosition(0);
    }

    private void hideTvViewForCustom() {
        this.tvPrompt.setBackgroundColor(Color.BLACK);
        this.tvView.setVisibility(View.INVISIBLE);
    }

    private void recoverTvViewForCustom() {
        this.tvView.setVisibility(View.VISIBLE);
        if (this.isRadioChannel) {
            this.tvPrompt.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_radio));
        } else {
            this.tvPrompt.setBackgroundDrawable((Drawable) null);
        }
        this.mTvHandler.sendEmptyMessage(0);
    }

    public void setTvViewElevation(float elevation) {
        this.tvView.setElevation(elevation);
        this.tvPrompt.setElevation(elevation);
        this.tvPrompt.bringToFront();
    }

    public void setTvViewPosition(int mode) {
        int left;
        int top;
        int right;
        int bottom;
        int duration;
        int transY = 0;
        this.tvViewMode = mode;
        switch (mode) {
            case 1:
                transY = -dipToPx(this, 545.0f);
                break;
            case 2:
                break;
            default:
                left = dipToPx(this, 120.0f);
                top = dipToPx(this, 197.0f);
                right = left + dipToPx(this, 310.0f);
                bottom = top + dipToPx(this, 174.0f);
                duration = 0;
                break;
        }
        left = dipToPx(this, 969.0f);
        top = dipToPx(this, 545.0f);
        right = left + dipToPx(this, 310.0f);
        bottom = top + dipToPx(this, 174.0f);
        duration = 500;
        HoverView.setViewPosition(this.tvView, new Rect(left, top, right, bottom));
        HoverView.setViewPosition(this.tvPrompt, new Rect(left, top, right, bottom));
        this.tvView.animate().translationY((float) transY).setDuration((long) duration).start();
        this.tvPrompt.animate().translationY((float) transY).setDuration((long) duration).start();
    }

    /* access modifiers changed from: private */
    public boolean isBootvideoStopped() {
        return true;
    }

    /* access modifiers changed from: private */
    public void tuneTvView() {
    }

    private void releaseTvView() {
        this.tvView.setVisibility(View.GONE);
    }

    /* access modifiers changed from: private */
    public void setTvPrompt(int mode) {
        switch (mode) {
            case 0:
                this.tvPrompt.setText((CharSequence) null);
                if (this.isRadioChannel) {
                    this.tvPrompt.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_radio));
                    return;
                } else {
                    this.tvPrompt.setBackground((Drawable) null);
                    return;
                }
            case 1:
                this.tvPrompt.setText(getResources().getString(R.string.str_no_signal));
                if (this.isRadioChannel) {
                    this.tvPrompt.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_radio));
                    return;
                } else {
                    this.tvPrompt.setBackground((Drawable) null);
                    return;
                }
            case 2:
                this.tvPrompt.setText(getResources().getString(R.string.str_scrambeled));
                if (this.isRadioChannel) {
                    this.tvPrompt.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_radio));
                    return;
                } else {
                    this.tvPrompt.setBackground((Drawable) null);
                    return;
                }
            case 3:
                this.tvPrompt.setText((CharSequence) null);
                this.tvPrompt.setBackgroundDrawable(getResources().getDrawable(R.drawable.hotplug_out));
                return;
            case 4:
                this.tvPrompt.setText((CharSequence) null);
                this.tvPrompt.setBackgroundDrawable(getResources().getDrawable(R.drawable.spdifin));
                return;
            default:
                return;
        }
    }

    public void stopMusicPlayer() {
        Intent intent = new Intent();
        intent.setAction("com.android.music.musicservicecommand.pause");
        intent.putExtra("command", "stop");
        sendBroadcast(intent);
    }

    public class TvViewInputCallback extends TvView.TvInputCallback {
        public TvViewInputCallback() {
        }

        public void onEvent(String inputId, String eventType, Bundle eventArgs) {
            Log.d("MediaBoxLauncher", "====onEvent==inputId =" + inputId + ", ===eventType =" + eventType);
        }

        public void onVideoAvailable(String inputId) {
            Launcher.this.setTvPrompt(0);
            Log.d("MediaBoxLauncher", "====onVideoAvailable==inputId =" + inputId);
        }

        public void onConnectionFailed(String inputId) {
            Log.d("MediaBoxLauncher", "====onConnectionFailed==inputId =" + inputId);
            new Thread(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(200);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (!TextUtils.isEmpty(Launcher.this.mTvInputId)) {
                        Launcher.this.tvView.tune(Launcher.this.mTvInputId, Launcher.this.mChannelUri);
                    }
                }
            }).start();
        }

        public void onVideoUnavailable(String inputId, int reason) {
            Log.d("MediaBoxLauncher", "====onVideoUnavailable==inputId =" + inputId + ", ===reason =" + reason);
        }
    }


}